<?php


namespace support\utils;


class TreeUtil
{
    static function getTree($items, $pidKey = 'pid', $idKey = "id")
    {
        $index_items = [];
        foreach ($items as $item) {
            $index_items[$item[$idKey]] = $item;
        }
        $tree = array();
        foreach ($index_items as $item) {
            //判断是否有数组的索引==
            if (isset($index_items[$item[$pidKey]])) {     //查找数组里面是否有该分类  如 isset($items[0])  isset($items[1])
                $index_items[$item[$pidKey]]['children'][] = &$index_items[$item[$idKey]]; //上面的内容变化,$tree里面的值就变化
            } else {
                $tree[] = &$index_items[$item[$idKey]];   //把他的地址给了$tree
            }
        }
        return $tree;
    }
}